
#include <stdlib.h>
#include <stdio.h>
#include "mpx_supt.h"
#include "commhand.h"
#include "r1.h"
#include "r2.h"
#include "r3.h"

char prompt[11];

//Author: Brandon Strickland
//Date: 2/7/13
//
//this reads in from the terminal
//the read in string is captured up to 63 cahracters and
//the rest should be ignored.
//from the start:
//there is > printed to the screen and it waits for input
//it takes the  string typed by the user and sends it to driverinclude
//
//
//there is a troubleshooting line that displays what the user typed in
//to make sure it is working correctly
void commhand()
{
	int maxSize, promptSize;
	char buffer[64];
	
	maxSize = 63;
	strcpy(prompt, ">");

	while(TRUE)
	{
		promptSize = strlen(prompt);
		sys_req(WRITE,TERMINAL, prompt, &promptSize);
		sys_req(READ,TERMINAL, buffer, &maxSize);
		if(driver(buffer)==-1)//sends the input to driver
			break; //break if exit
	}
}


//Author: Brandon Strickland
//Date: 2/7/13
//
//this takes what commhand sends it and runs through all the functions
//and finds out if it was of correct grammar and syntax.
//
//EDITED BY: Billy Hardy 2/11/13 (tokenized buffer)(billy has really hard
//code to read :/ )
//
//when adding it this, you must change NUM_FUNCTIONS.
int driver(char *buffer)
{
	int i;
	int (*p[NUM_FUNCTIONS]) (int, char *[]);
	int check;
	char *c;
	int argc;
	char *argv[32];

	p[0] = &version;
	p[1] = &date;
	p[2] = &quit;
	p[3] = &help;
	p[4] = &dir;
	p[5] = &createPCB;
	p[6] = &renameCommand;
	p[7] = &deletePCB;
	p[8] = &blockPCB;
	p[9] = &unblockPCB;
	p[10] = &suspendPCB;
	p[11] = &resumePCB;
	p[12] = &setPCBPriority;
	p[13] = &showPCB;
	p[14] = &showReady;
	p[15] = &showBlocked;
	p[16] = &showAll;
	p[17] = &dispatching;
	p[18] = &loadprocs;
	p[NUM_FUNCTIONS-1] = &invalid;
	c = strtok(buffer, " \n\t");
	argc = 0;

	/////////////////////////////////////////////////////////////////
	//this is what tokenizes the input by the user                 //
	//billy's baby                                                 //
	while(c != NULL)                                               //
	{                                                              //
		sprintf(argv[argc], c);//argc is the number of parms       //
		c = strtok(NULL, " \n\t");                                 //
		argc++;                                                    //
	}                                                              //
	/////////////////////////////////////////////////////////////////

	i=0;
	while(TRUE)
	{
		check = p[i](argc, argv);
		if(check != 0)
			return check;
		i++;
	}
}

//Author: Billy
//Date: 2/21
//Allows renaming of prompt
//PreConditions: 3 words, "rename prompt" and then the new prompt, at most 10 characters
//PostConditions: Prompt is changed
//NOTE: Ability to expand this to aliasing of commands
int renameCommand(int argc, char *argv[]) {
	if(strcmp(argv[0], "rename") != 0)
		return 0;
	if(argc != 3)
		printf("Invalid renaming syntax\n");
	else if(strcmp(argv[1], "prompt") == 0) {
		if(strlen(argv[2]) > 10)
			printf("Prompt can be at most 10 characters long\n");
		else strcpy(prompt, argv[2]);
	}
	return 1;
}

